/* All home page scripts are here */
$(document).ready(function() {

	/*This function is commented because I have changed behavior to button click and not scroll.*/
  /*$(window).scroll(function(){
	$("#ajax-loading").hide();
    if ($(window).scrollTop() == $(document).height() - $(window).height()){
      load_more_articles();
    }
  });*/

	$('#ajax-loading img').on('click', function() {
		load_more_articles();
	});

  $("#ajax-loading").ajaxSend(function() {
    $(this).fadeIn('fast');
  }).ajaxSuccess(function() {
  });
});

function load_more_articles(numberOfArticles) {
  var articles = $('.nodes article');
  var numberOfArticles = articles.length;

  $.ajax({
    url: base_url + "ajax/hom_ajax_more_stories/" + numberOfArticles,
    async: false,
    dataType: "html",
    success: function(data) {
      if (data != 'end') {
        $('.content-container .row .nodes').append(data);
        var eventCategory = 'Infinite Scroll';
        var eventAction = 'scroll offset ' + numberOfArticles;
        var eventLabel = 'Infinite scroll home page';
        _gaq.push(['_trackEvent', eventCategory, eventAction, eventLabel]);
        _gaq.push(['_trackPageview','homemore='+numberOfArticles]);
      }
      else {
        alert('End of all articles');
	      $('#ajax-loading').remove();
        _gaq.push(['_trackEvent', 'Infinite Scroll', 'scroll end', 'End of all articles']);
      }
    }
  });
}